package com.skt.common_hr.teamprofile.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.teamprofile.TeamProfileFacade;
	import com.skt.common_hr.teamprofile.model.ProfileDetailProxy;
	import com.skt.common_hr.teamprofile.model.ProfileProxy;
	import com.skt.common_hr.teamprofile.view.components.OpinionView;
	import com.skt.common_hr.teamprofile.view.components.OverView;
	import com.skt.common_hr.teamprofile.view.components.TabSheeta;
	
	import flash.events.Event;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	public class ProfileMediator extends CommonMediator
	{
		public static const NAME:String = "ProfileMediator"; 


		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function ProfileMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		override protected function initializeView():void
 		{
 			setCountry();
			view.tsa.addEventListener(TabSheeta.EVENT_VIEW_OPINION, onViewOpinion);
 		}
		
		private function setCountry():void
		{
			if(Variables.country!="CN") {
				view.ts1cn.tabVisible = false;
				view.ts5cn.tabVisible = false;
				view.tsCn01.tabVisible = false;
			}
			else {
				view.ts1.tabVisible = false;
				view.ts5.tabVisible = false;
				view.tabNavi.selectedIndex = 1;
			}
		}

		private function loadProfile(pernr:String):void
		{
 			var basicProxy:ProfileProxy = facade.retrieveProxy(ProfileProxy.NAME) as ProfileProxy;
 			basicProxy.load(pernr);
 			
 			var detailProxy:ProfileDetailProxy = facade.retrieveProxy(ProfileDetailProxy.NAME) as ProfileDetailProxy;
			detailProxy.load(pernr);
		}






		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					TeamProfileFacade.CHANGE_SELECTED_MEMBER,
					TeamProfileFacade.LOAD_PROFILE_COMPLETE,
					TeamProfileFacade.LOAD_PROFILE_FAILED,
					TeamProfileFacade.LOAD_PROFILE_DETAIL_COMPLETE,
					TeamProfileFacade.LOAD_PROFILE_DETAIL_FAILED
					];
		}
		
		
		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case TeamProfileFacade.CHANGE_SELECTED_MEMBER:
					view.callLater(loadProfile, [String(note.getBody())]);
					break;
				case TeamProfileFacade.LOAD_PROFILE_COMPLETE:
					onSucceedDataLoad(note.getBody() as ProfileProxy);
					break;
				case TeamProfileFacade.LOAD_PROFILE_FAILED:
					break;
				//개인신상
				case TeamProfileFacade.LOAD_PROFILE_DETAIL_COMPLETE:
					onSucceedDetailDataLoad(note.getBody() as ProfileDetailProxy);
					break;
				case TeamProfileFacade.LOAD_PROFILE_DETAIL_FAILED:
					trace("LOAD_PROFILEDETAIL_FAILED");
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}


		

		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - load된 데이터를 화면에 출력
		 * @param
		 * @return void
		 */
 		private function onSucceedDataLoad(proxy:ProfileProxy):void
 		{
 			trace("onSucceedDataLoad");
 			view.dataProvider = proxy.profile;
 			view.ts1.currentState = "";
 			//setStatusMessage("데이터가 정상적으로 로드되었습니다."); 
 		}
 		private function onSucceedDetailDataLoad(proxy:ProfileDetailProxy):void
 		{
 			if(Variables.country!="CN") {
	 			view.ts1.dataProvider = proxy.privateDetail;
	 			view.ts5.dataProvider = proxy.family;
 			}
 			else {
	 			view.ts1cn.dataProvider = proxy.privateDetail;
	 			view.ts5cn.dataProvider = proxy.family;
	 			view.tsCn01.dataProvider = proxy.cn01;
 			}

 			view.ts2.dataProvider = proxy.officalAnn;
 			view.ts3.dataProvider = proxy.career;
 			view.ts4.dataProvider = proxy.scholarship;
 			view.ts7.dataProvider = proxy.education;
 			view.ts8.dataProvider = proxy.language;
 			view.ts9.dataProvider = proxy.license;
 			view.tsa.dataProvider = proxy.health;
 		}

 		private var opinionView:OpinionView = null;
 		private function onViewOpinion(e:Event):void
 		{
 			//trace(view.dg1.selectedIndex);
 			if(opinionView == null) {
 				opinionView = PopUpManager.createPopUp(view.root, OpinionView, true) as OpinionView;
 				PopUpManager.centerPopUp(opinionView);
 			}
 			else {
 				PopUpManager.addPopUp(opinionView, view.root, true);
 			}
 			opinionView.setFocus();
 			opinionView.opinion = view.tsa.dataProvider.getItemAt(view.tsa.dga.selectedIndex).OPINION;
 		}
 		









		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():OverView
        {
            return viewComponent as OverView;
        }
	}
}